import { useEffect, useRef, useState } from "react";
import { motion as m } from "framer-motion";
import Skeleton from "react-loading-skeleton";
import { useRouter } from "next/router";
import Link from "next/link";
import Head from "next/head";
import Footer from "@/components/shared/footer";
import Image from "next/image";
import { aniAdvanceSearch } from "@/lib/anilist/aniAdvanceSearch";
import MultiSelector from "@/components/search/dropdown/multiSelector";
import SingleSelector from "@/components/search/dropdown/singleSelector";
import {
animeFormatOptions,
formatOptions,
genreOptions,
mangaFormatOptions,
mediaType,
seasonOptions,
tagsOption,
yearOptions,
} from "@/components/search/selection";
import InputSelect from "@/components/search/dropdown/inputSelect";
import { Cog6ToothIcon, TrashIcon } from "@heroicons/react/20/solid";
import useDebounce from "@/lib/hooks/useDebounce";
import { NewNavbar } from "@/components/shared/NavBar";
import MobileNav from "@/components/shared/MobileNav";
import SearchByImage from "@/components/search/searchByImage";
import { PlayIcon } from "@heroicons/react/24/outline";
export async function getServerSideProps(context) {
const { param } = context.query;
const { search, format, genres, season, year } = context.query;
let getFormat, getSeason, getYear;
let getGenres = [];
if (genres) {
const gr = genreOptions.find(
(i) => i.value.toLowerCase() === genres.toLowerCase()
);
getGenres.push(gr);
}
if (season) {
getSeason = seasonOptions.find(
(i) => i.value.toLowerCase() === season.toLowerCase()
);
if (!year) {
const now = new Date().getFullYear();
getYear = yearOptions.find((i) => i.value === now.toString());
} else {
getYear = yearOptions.find((i) => i.value === year);
}
}
if (format) {
getFormat = formatOptions.find(
(i) => i.value.toLowerCase() === format.toLowerCase()
);
}
if (!param && param.length !== 1) {
return {
notFound: true,
};
}
const typeIndex = param[0] === "anime" ? 0 : 1;
return {
props: {
index: typeIndex,
query: search || null,
formats: getFormat || null,
seasons: getSeason || null,
years: getYear || null,
genres: getGenres || null,
},
};
}
export default function Card({
index,
query,
genres,
formats,
seasons,
years,
}) {
const inputRef = useRef(null);
const router = useRouter();
const [data, setData] = useState();
const [imageSearch, setImageSearch] = useState();
const [loading, setLoading] = useState(true);
const [search, setQuery] = useState(query);
const debounceSearch = useDebounce(search, 500);
const [type, setSelectedType] = useState(mediaType[index]);
const [year, setYear] = useState(years);
const [season, setSeason] = useState(seasons);
const [sort, setSelectedSort] = useState();
const [genre, setGenre] = useState(genres);
const [format, setFormat] = useState(formats);
const [isVisible, setIsVisible] = useState(false);
const [page, setPage] = useState(1);
const [nextPage, setNextPage] = useState(true);
async function advance() {
setLoading(true);
const data = await aniAdvanceSearch({
search: debounceSearch,
type: type?.value,
genres: genre,
page: page,
sort: sort?.value,
format: format?.value,
season: season?.value,
seasonYear: year?.value,
});
if (data?.media?.length === 0) {
setNextPage(false);
setLoading(false);
} else if (data !== null && page > 1) {
setData((prevData) => {
return [...(prevData ?? []), ...data?.media];
});
setNextPage(data?.pageInfo.hasNextPage);
setLoading(false);
} else {
setData(data?.media);
setNextPage(data?.pageInfo.hasNextPage);
setLoading(false);
}
}
useEffect(() => {
setData(null);
setPage(1);
setNextPage(true);
advance();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
debounceSearch,
type?.value,
sort?.value,
genre,
format?.value,
season?.value,
year?.value,
]);
useEffect(() => {
if (imageSearch) return;
advance();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [page, imageSearch]);
useEffect(() => {
function handleScroll() {
if (imageSearch) {
window.removeEventListener("scroll", handleScroll);
return;
}
if (page > 10 || !nextPage) {
window.removeEventListener("scroll", handleScroll);
return;
}
if (
window.innerHeight + window.pageYOffset >=
document.body.offsetHeight - 3
) {
setPage((prevPage) => prevPage + 1);
}
}
window.addEventListener("scroll", handleScroll);
return () => window.removeEventListener("scroll", handleScroll);
}, [page, nextPage, imageSearch]);
const handleKeyDown = async (event) => {
if (event.key === "Enter") {
event.preventDefault();
const inputValue = event.target.value;
if (inputValue === "") {
setQuery(null);
} else {
setQuery(inputValue);
}
}
};
function trash() {
setImageSearch();
setQuery();
setGenre();
setFormat();
setSelectedSort();
setSeason();
setYear();
router.push(`/en/search/${mediaType[index]?.value?.toLowerCase()}`);
}
function handleVisible() {
setIsVisible(!isVisible);
}
const handleVideoHover = (hovered, id) => {
const updatedImageSearch = imageSearch?.map((item) => {
if (item.filename === id) {
return { ...item, hovered };
}
return item;
});
setImageSearch(updatedImageSearch);
};
// console.log({ loading, data });
return (
<>
Moopa - search
{/* GENRES */}
{/* SORT */}
{/* */}
{/* FORMAT */}
{/* SEASON */}
{/* YEAR */}
{isVisible && (
{/* GENRES */}
{/* SORT */}
{/* */}
{/* FORMAT */}
{/* SEASON */}
{/* YEAR */}
)}
{/*
*/}
{loading
? ""
: !data && (
Oops!
Nothing's Found...
)}
{data &&
data?.length > 0 &&
!imageSearch &&
data?.map((anime, index) => {
const anilistId = anime?.mappings?.find(
(x) => x.providerId === "anilist"
)?.id;
return (
{anime.status === "RELEASING" ? (
) : anime.status === "NOT_YET_RELEASED" ? (
) : null}
{anime.title.userPreferred}
{anime.format ||
-
} ·{" "}
{anime.status || -
} ·{" "}
{anime.episodes
? `${anime.episodes || "N/A"} Episodes`
: `${anime.chapters || "N/A"} Chapters`}
);
})}
{loading && (
<>
{[1, 2, 4, 5, 6, 7, 8].map((item) => (
))}
>
)}
{imageSearch && (
{imageSearch.map((a, index) => {
return (
{
handleVideoHover(true, a.filename);
}}
onMouseLeave={() => handleVideoHover(false, a.filename)}
>
{a?.image && (
)}
{a?.video && (
)}
{/* {a.title}
*/}
{a?.anilist.title.romaji}
{" "}
| Episode {a.episode}
);
})}
)}
{!loading && page > 10 && nextPage && (
)}
{/*
*/}
>
);
}